/* test_base.c
 test for init:
sync with disk_alloc(), disk_free()
XXX: Poor Performance: too many reclam&get_buddy @ test_base.gcov
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "buddy_system.h"
#include "defines.h"

int	disk_need[15]={1,2,4,4,4,89,160,2,43,34,33,3,33,21,150};
int	disk_location[15];

//	state=disk_init(INIT_TYPE_CREATE, g_image, NULL, "buddy.log", 50*1024*64-DISK_IMAGE_SIZE-DISK_SEG_SIZE);
int main()
{
	char* g_image;
	STATE state;
	int i;

	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	sync_init("/tmp/disk", "sync.log");
	state=disk_init(INIT_TYPE_CREATE, g_image, NULL, "buddy.log", 50*1024*64-DISK_IMAGE_SIZE-DISK_SEG_SIZE);

	disk_show();
	for (i=0; i<15; i++)
	{
		disk_location[i]=disk_alloc(disk_need[i]);
	}		
	disk_show();

	disk_exit();
	state=sync_write(DISK_IMAGE, g_image, IMAGE_K_SIZE*1024);
	if (state==-1) printf ("state error\n");
	mp_free(g_image);



	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	sync_read(DISK_IMAGE, g_image, IMAGE_K_SIZE*1024);
	state=disk_init(INIT_TYPE_LOAD, g_image, NULL, "buddy.log", 50*1024*64-DISK_IMAGE_SIZE-DISK_SEG_SIZE);
	
	disk_show();
	for (i=0; i<15; i++)
	{
		disk_free(disk_location[i]);
	}
	disk_show();
		
	disk_exit();
	state=sync_write(DISK_IMAGE, g_image, IMAGE_K_SIZE*1024);
	sync_exit();
	mp_free(g_image);

	return 0;
}
